<?php
namespace app\admin\controller;

use think\Db;
use app\common\controller\AdminBase;
use app\common\model\Xdmsg as XdmsgModel;

class Order extends AdminBase
{
    public function index($cid,$page = 1){
        $map = [
            'cid' => $cid
        ];

        $check_author = self::checkAuthor($cid,$this->admin_id);
        
        if($check_author['code'] == 0) return $this->error('无权限操作');
        
        $total = Db::name('xdmsg')->where($map)->count();
        $order_list = Db::name('xdmsg')->where($map)->paginate(500,false,['page'=>$page]);

        $this->assign('order_list',$order_list);
        $this->assign('total',$total);
        $this->assign('admin_id',$this->admin_id);
        $this->assign('phone','13123456789');
        return $this->fetch('index',['cid'=>$cid,'page'=>$page]);
    }

    public function clear_all($cid){

        $map = [
            'cid' => $cid
        ];

        $check_author = self::checkAuthor($cid,$this->admin_id);
        if($check_author['code'] == 0) return $this->error('无权限操作');

        $xdmsg_model = new XdmsgModel();
        $result = $xdmsg_model->where($map)->delete();

        if($result){
            $this->success('清空成功');
        }else{
            $this->error('清空失败');
        }
    }

    /** 
     * 创建(导出)Excel数据表格 
     * @param  array   $list        要导出的数组格式的数据 
     * @param  string  $filename    导出的Excel表格数据表的文件名 
     * @param  array   $indexKey    $list数组中与Excel表格表头$header中每个项目对应的字段的名字(key值) 
     * @param  array   $startRow    第一条数据在Excel表格中起始行   
     * @param  [bool]  $excel2007   是否生成Excel2007(.xlsx)以上兼容的数据表 
     * 比如: $indexKey与$list数组对应关系如下: 
     *     $indexKey = array('id','username','sex','age'); 
     *     $list = array(array('id'=>1,'username'=>'YQJ','sex'=>'男','age'=>24)); 
     */  
    function exportExcel($filename = '',$startRow=1,$excel2007=false){
        //文件引入  
        require_once EXTEND_PATH .'excel/PHPExcel/IOFactory.php';  
        require_once EXTEND_PATH.'excel/PHPExcel.php';  
        require_once EXTEND_PATH.'excel/PHPExcel/Writer/Excel2007.php'; 
        
        $list = $this->request->param();
        $map['cid'] = ['eq',$list['cid']];
        $xdmsg_model = new XdmsgModel();
        $count_detail_list = $xdmsg_model->field('name,phone,address,xdip,xdref,xdtime')->where($map)->order('xdtime desc')->select();
        
        $indexKey = array('name','phone','address','xdip','xdref','xdtime'); 
        $list_data = array(array('name'=>'用户姓名','phone'=>'用户手机','address'=>'用户地址','xdip'=>'用户ip','xdref'=>'用户来源','xdtime'=>'下单时间')); 
        foreach($count_detail_list as $v){
            if($this->admin_id != 1){
                $v['name'] = '***';
                $v['address'] = '***';
                $v['phone'] = '****'.substr($v['phone'],4);
            }
            $list_data[] = $v;
        }
    
    
    
        // dump($list_data);exit();
    
        if(empty($filename)) $filename = 'order_detail_'.date('m-d');  
        if( !is_array($indexKey)) return false;  
    
        $header_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');  
        //初始化PHPExcel()
        $objPHPExcel = new \PHPExcel();  
        
        //设置保存版本格式  
        if($excel2007){  
            $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);  
            $filename = $filename.'.xlsx';  
        }else{  
            $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);  
            $filename = $filename.'.xls';  
        }  

          
        //接下来就是写数据到表格里面去  
        $objActSheet = $objPHPExcel->getActiveSheet();  
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
    
        $objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray(array('font' => array ('bold' => true)));
        $objPHPExcel->getActiveSheet()->getStyle('B1')->applyFromArray(array('font' => array ('bold' => true)));
        $objPHPExcel->getActiveSheet()->getStyle('C1')->applyFromArray(array('font' => array ('bold' => true)));
        $objPHPExcel->getActiveSheet()->getStyle('D1')->applyFromArray(array('font' => array ('bold' => true)));
        $objPHPExcel->getActiveSheet()->getStyle('E1')->applyFromArray(array('font' => array ('bold' => true)));
        $objPHPExcel->getActiveSheet()->getStyle('F1')->applyFromArray(array('font' => array ('bold' => true)));
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(13);
        $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(13);
        $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(13);
        $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(13);
        $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(13);
        $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(13);
    
        //$startRow = 1;  
        foreach ($list_data as $k=>$row) {
            foreach ($indexKey as $key => $value){
                //这里是设置单元格的内容  
                $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]);  
            }  
            $startRow++;
        }
          
        // 下载这个表格，在浏览器输出  
        header("Pragma: public");  
        header("Expires: 0");  
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");  
        header("Content-Type:application/force-download");  
        header("Content-Type:application/vnd.ms-execl");  
        header("Content-Type:application/octet-stream");  
        header("Content-Type:application/download");;  
        header('Content-Disposition:attachment;filename='.$filename.'');  
        header("Content-Transfer-Encoding:binary");  
        $objWriter->save('php://output');  
    }

    private static function checkAuthor($id,$admin_id){
        if($admin_id != 1){
            $map = [
                'id' => $id,
                'admin_id' => $admin_id
            ];
            $check = Db::name('domain')->where($map)->find();
            if(!$check){
                return ['code' => 0];
            }
        }
        return ['code' => 1];
    }
}